package org.suyafei.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.suyafei.controller.request.LinkGroupAddRequest;
import org.suyafei.controller.request.LinkGroupUpdateRequest;
import org.suyafei.enums.BizCodeEnum;
import org.suyafei.service.LinkGroupService;
import org.suyafei.utils.JsonData;
import org.suyafei.vo.LinkGroupVO;

import java.util.List;

/**
 * @author 宿某人
 * @Date: 2021/12/17/5:08 下午
 * @Description:
 */
@RestController
@RequestMapping("api/group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;

    /**
     * 创建分组
     *
     * @param addRequest
     * @return
     */
    @PostMapping("/add")
    public JsonData add(@RequestBody LinkGroupAddRequest addRequest) {
        int rows = linkGroupService.add(addRequest);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }


    @DeleteMapping("/del/{group_id}")
    public JsonData delete(@PathVariable("group_id") Long groupId) {
        int rows = linkGroupService.del(groupId);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_DEL_FAIL);
    }

    /**
     * 查询分组详情
     * @param groupId
     * @return
     */
    @GetMapping("/detail/{group_id}")
    public JsonData detail(@PathVariable("group_id") Long groupId) {
        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }

    /**
     * 获取用户全部分组
     * @return
     */
    @GetMapping("/list")
    public JsonData findUserAllLinkGroup(){
        List<LinkGroupVO> linkGroupVOList = linkGroupService.listAllLinkGroup();
        return JsonData.buildSuccess(linkGroupVOList);

    }

    /**
     * 更新用户分组
     * @param request
     * @return
     */
    @PutMapping("/update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest request) {
        int rows = linkGroupService.updateLinkGroup(request);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_UPDATE_FAIL);

    }
}
